
var config = {
    webWorkerPath:'lib/cornerstoneWADOImageLoaderWebWorker.js',
    taskConfiguration:{
         'decodeTask':{
            codecsPath:'cornerstoneWADOImageLoaderCodecs.js'
        }
    }
};
cornerstoneWADOImageLoader.webWorkerManager.initialize(config);

cornerstoneWADOImageLoader.configure({
    beforeSend: function(xhr) {
        // Add custom headers here (e.g. auth tokens)
        //xhr.setRequestHeader('x-auth-token', 'my auth token');
    }
});

function loadLocalImages(paths) {
    loadImage(studyViewerTemplate, viewportModelTemplate, paths);
}

function fullScreen() { // 全屏展示
    var el = document.documentElement;
    var rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullScreen;
    if(typeof rfs != "undefined" && rfs) {
        rfs.call(el);
    } else if(typeof window.ActiveXObject != "undefined") {
        var wscript = new ActiveXObject("WScript.Shell");
        if(wscript != null) {
            wscript.SendKeys("{F11}");
        }
    }
}

function activate(id) {
    $('div').removeClass('active');
    $(id).addClass('active');
}

function disableAllTools() { // 控件失效
    cornerstoneTools.wwwc.disable(element);
    cornerstoneTools.pan.activate(element, 2); // 2 is middle mouse button
    cornerstoneTools.zoom.activate(element, 4); // 4 is right mouse button
    cornerstoneTools.probe.deactivate(element, 1);
    cornerstoneTools.length.deactivate(element, 1);
    cornerstoneTools.ellipticalRoi.deactivate(element, 1);
    cornerstoneTools.rectangleRoi.deactivate(element, 1);
    cornerstoneTools.angle.deactivate(element, 1);
    cornerstoneTools.rotate.deactivate(element, 1);
    cornerstoneTools.arrowAnnotate.deactivate(element, 1);
    cornerstoneTools.arrowAnnotateTouch.deactivate(element);
}

function setupGlobalButtons() {
    // open local files
    $('#uploadFile').on('change', function(e) {
        var files = e.target.files;
        loadLocalImages(files);
    })

    $('#logOut').on('click touchstart', function() {
        if(window.confirm("确定退出吗?", "提示", {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            closeOnClickModal: false,
            type: 'warning'
        })) {
            window.location.href = "about:blank";
            window.close();
        }
    })
}

function setupToolButtons() {
    // Tool button event handlers that set the new active tool

    // WW/WL
    $('#enableWindowLevelTool').on('click touchstart', function() {
        disableAllTools();
        forEachViewport(function(element) {
            cornerstoneTools.wwwc.activate(element, 1);
            cornerstoneTools.wwwcTouchDrag.activate(element);
        });
    });

    // Invert
    $(buttons[1]).on('click touchstart', function() {
        disableAllTools();
        forEachViewport(function(element) {
            var viewport = cornerstone.getViewport(element);
            // Toggle invert
            if (viewport.invert === true) {
                viewport.invert = false;
            } else {
                viewport.invert = true;
            }
            cornerstone.setViewport(element, viewport);
        });
    });

    // Zoom
    $('#zoom').on('click touchstart', function() {
        disableAllTools();
        forEachViewport(function(element) {
            cornerstoneTools.zoom.activate(element, 5); // 5 is right mouse button and left mouse button
            cornerstoneTools.zoomTouchDrag.activate(element);
        });
    });

    // Pan
    $('#pan').on('click touchstart', function() {
        disableAllTools();
        forEachViewport(function(element) {
            cornerstoneTools.pan.activate(element, 3); // 3 is middle mouse button and left mouse button
            cornerstoneTools.panTouchDrag.activate(element);
        });
    });

    // // Stack scroll
    // $(buttons[4]).on('click touchstart', function() {
    //     disableAllTools();
    //     forEachViewport(function(element) {
    //         cornerstoneTools.stackScroll.activate(element, 1);
    //         cornerstoneTools.stackScrollTouchDrag.activate(element);
    //     });
    // });

    // Length measurement
    $('#enableLength').on('click touchstart', function() {
        disableAllTools();
        forEachViewport(function(element) {
            cornerstoneTools.length.activate(element, 1);
        });
    });

    // Angle measurement
    $('#angle').on('click touchstart', function() {
        disableAllTools();
        forEachViewport(function(element) {
            cornerstoneTools.angle.activate(element, 1);
        });
    });

    // Pixel probe
    $('#probe').on('click touchstart', function() {
        disableAllTools();
        forEachViewport(function(element) {
            cornerstoneTools.probe.activate(element, 1);
        });
    });

    // Elliptical ROI
    $('#circleroi').on('click touchstart', function() {
        disableAllTools();
        forEachViewport(function(element) {
            cornerstoneTools.ellipticalRoi.activate(element, 1);
        });
    });

    // Rectangle ROI
    $('#rectangleroi').on('click touchstart', function() {
        disableAllTools();
        forEachViewport(function (element) {
            cornerstoneTools.rectangleRoi.activate(element, 1);
        });
    });

    $("#softTissue").click(function() {
        forEachViewport(function(element) {
            var viewport = cornerstone.getViewport(element);
            viewport.voi.windowWidth = 500;
            viewport.voi.windowCenter = 60;
            cornerstone.setViewport(element, viewport);
        })
    });

    $('#lung').click(function() {
        forEachViewport(function(element) {
            var viewport = cornerstone.getViewport(element);
            viewport.voi.windowWidth = 1600;
            viewport.voi.windowCenter = -600;
            cornerstone.setViewport(element, viewport);
        })
    });

    $('#bone').click(function() {
        forEachViewport(function(element) {
            var viewport = cornerstone.getViewport(element);
            viewport.voi.windowWidth = 2000;
            viewport.voi.windowCenter = 300;
            cornerstone.setViewport(element, viewport);
        })
    });

    // // Play clip
    // $(buttons[10]).on('click touchstart', function() {
    //     forEachViewport(function(element) {
    //       var stackState = cornerstoneTools.getToolState(element, 'stack');
    //       var frameRate = stackState.data[0].frameRate;
    //       // Play at a default 10 FPS if the framerate is not specified
    //       if (frameRate === undefined) {
    //         frameRate = 10;
    //       }
    //       cornerstoneTools.playClip(element, frameRate);
    //     });
    // });

    // // Stop clip
    // $(buttons[11]).on('click touchstart', function() {
    //     forEachViewport(function(element) {
    //         cornerstoneTools.stopClip(element);
    //     });
    // });

    // // Tooltips
    // $(buttons[0]).tooltip();
    // $(buttons[1]).tooltip();
    // $(buttons[2]).tooltip();
    // $(buttons[3]).tooltip();
    // $(buttons[4]).tooltip();
    // $(buttons[5]).tooltip();
    // $(buttons[6]).tooltip();
    // $(buttons[7]).tooltip();
    // $(buttons[8]).tooltip();
    // $(buttons[9]).tooltip();
    // $(buttons[10]).tooltip();
    // $(buttons[11]).tooltip();
    // $(buttons[12]).tooltip();

};